<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">楼座管理</span>
    <el-button @click="dialogVisible=true" type="primary"
               style="float:right;
               margin-top:13px;
               ">新建楼座</el-button>
  </div>
  <!-- 新增楼座弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose" style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="楼座名称">
            <el-input placeholder="请输入内容" v-model="saveBuilding.bname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建成日期">
            <el-date-picker type="date"
                placeholder="请选择"
                v-model="saveBuilding.bcreatTime"
                style="width: 100%;"
                            value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="房屋用途">
            <el-select placeholder="请选择" v-model="saveBuilding.buse">
              <el-option label="普通住宅" value="10"></el-option>
              <el-option label="商业房产" value="20"></el-option>
              <el-option label="公寓" value="30"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单元数量">
            <el-select placeholder="请选择" v-model="saveBuilding.bunitNum">
              <el-option label="1" value="1"></el-option>
              <el-option label="2" value="2"></el-option>
              <el-option label="3" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单元楼层数量">
            <el-select placeholder="请选择" v-model="saveBuilding.bfloorNum">
              <el-option label="1" value="1"></el-option>
              <el-option label="2" value="2"></el-option>
              <el-option label="3" value="3"></el-option>
              <el-option label="4" value="4"></el-option>
              <el-option label="5" value="5"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="saveBuild">确定</el-button>
      <el-button @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
  <!-- 楼座搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="房屋用途">
        <el-select placeholder="请选择" style="width:220px;" v-model="selectBuilding.bUse">
          <el-option label="普通住宅" value="10"></el-option>
          <el-option label="商业房产" value="20"></el-option>
          <el-option label="公寓" value="30"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼座名称" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="selectBuilding.bName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadBuilding">查询</el-button>
        <el-button @click="reSearch">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 楼座列表 -->
  <el-card style="margin:20px;">
    <el-table :data="buildingArr">
      <el-table-column type="index" prop="bid" label="编号" width="80" align="center"/>
      <el-table-column prop="bname" label="楼座名称" width="80" align="center"/>
      <el-table-column prop="buse" label="房屋用途" align="center" :formatter="useFormatter"/>
      <el-table-column prop="bcreatTime" label="建成日期" align="center"/>
      <el-table-column prop="bunitNum" label="单元数量" align="center"/>
      <el-table-column prop="bfloorNum" label="楼层数量" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editBuilding(scope.row.bid)">编辑</el-button>
          <el-button link type="danger" size="small" @click="delBuilding(scope.row.bid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const dialogTitle = ref('新增楼座');
const dialogVisible = ref(false);

const handleClose = ()=>{
  if (confirm('是否关闭该弹窗?')){
    dialogVisible.value = false;
    saveBuilding.value = {};
  }
}

//保存新增楼座弹窗内容
const saveBuilding = ref({
  bname: '',
  bcreatTime: '',
  buse: '',
  bunitNum: '',
  bfloorNum: ''
})

//房屋用途值
const useFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 10){
    cellValue = "普通住宅";
  }else if (cellValue == 20){
    cellValue = "商业房产";
  }else if (cellValue == 30){
    cellValue = "公寓";
  }
  return cellValue;
}

//保存楼座列表数据
const buildingArr = ref([]);

//保存\更新楼座信息
const saveBuild = ()=>{
  let data = qs.stringify(saveBuilding.value)
  axios.post(BASE_URL + '/v1/building/save', data)
      .then((response)=>{
        if (response.data.code == 2000){
          saveBuilding.value = {};
          dialogVisible.value = false;
          ElMessage.success('保存成功');
          loadBuilding();
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

//保存搜索条件
const selectBuilding = ref({
  bUse: '',
  bName: ''
})

//一进入页面显示楼座信息
onMounted(()=>{
  loadBuilding();
})

//查询楼座
const loadBuilding = ()=>{
  let data = qs.stringify(selectBuilding.value);
  axios.get(BASE_URL + '/v1/building/select?' + data)
      .then((response)=>{
        if (response.data.code == 2000){
          buildingArr.value = response.data.data;
          console.log(buildingArr.value);
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

//重置
const reSearch = ()=>{
  selectBuilding.value = {};
  loadBuilding();
}

//编辑楼座信息
const editBuilding = (bid)=>{
  console.log(bid);
  dialogVisible.value = true;
  dialogTitle.value = '编辑楼座';
  axios.get(BASE_URL + '/v1/building/select?bid=' + bid)
      .then((response)=>{
        if (response.data.code == 2000){
          saveBuilding.value = response.data.data[0];
          console.log(saveBuilding.value);
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

//删除楼座信息
const delBuilding = (bid)=>{

  axios.post(BASE_URL + '/v1/building/delete/' + bid)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success('删除成功');
          loadBuilding();
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
</script>

<style scoped>

</style>